package com.hibernate.persistence.lifecycle;

import java.util.Random;

import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class LoadGetEntity {

	private static SessionFactory factory = new Configuration().configure().buildSessionFactory();
	private static Session session;
	private static Random rd = new Random();

	public static void main(String[] args) {
		// ## Load ,it will throw exception when it can't find from DB with Id
		System.out.println("--- LOAD ---");
		session = factory.openSession();
		System.out.println("session Open:" + session.toString());
		Message msg = (Message) session.load(Message.class, 3);
		Message msg2 = (Message) session.load(Message.class, 4);
		
		try{
			Message msg3 = (Message) session.load(Message.class, 100);
			System.out.println(msg3);
		}catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("session Load:" + session.toString());
		System.out.println("Before(Msg1):Id=" + session.getIdentifier(msg) + ", msgText=" + msg.toString());
		System.out.println("Before(Msg2):Id=" + session.getIdentifier(msg2) + ", msgText=" + msg2.toString());
		msg.setMessageText(msg.getMessageText() + " " + rd.nextInt(100));
		msg2.setMessageText(msg2.getMessageText() + " " + rd.nextInt(100));
		System.out.println("Update(Msg1):Id=" + session.getIdentifier(msg) + ", msgText=" + msg.toString());
		System.out.println("Update(Msg2):Id=" + session.getIdentifier(msg2) + ", msgText=" + msg2.toString());

		// session.beginTransaction(); // If it isn't have Transaction, it will
		// be not update.
		session.update("Message", msg);
		session.update("Message", msg2);

		System.out.println("Session:isConnected=" + session.isConnected() + ", isDefaultReadOnly="
				+ session.isDefaultReadOnly() + ", isDirty=" + session.isDirty() + ", isOpen=" + session.isOpen()
				+ ", isReadOnly Msg=" + session.isReadOnly(msg) + ", isReadOnly Msg2=" + session.isReadOnly(msg2));
		System.out.println("session Update:" + session.toString());

		// session.getTransaction().commit();
		session.close();
		// ## Get , it will return null when it can't find from DB with Id
		
		System.out.println("--- GET ---");
		session = factory.openSession();
		System.out.println("session Open:" + session.toString());
		msg = (Message) session.get(Message.class, 3);
		msg2 = (Message) session.get(Message.class, 4);
		try{
			Message msg3 = (Message) session.get(Message.class, 100);
			System.out.println(msg3);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("session Get:" + session.toString());
		System.out.println("Before(Msg1):Id=" + session.getIdentifier(msg) + ", msgText=" + msg.toString());
		System.out.println("Before(Msg2):Id=" + session.getIdentifier(msg2) + ", msgText=" + msg2.toString());
		msg.setMessageText(msg.getMessageText() + " " + rd.nextInt(100));
		msg2.setMessageText(msg2.getMessageText() + " " + rd.nextInt(100));
		System.out.println("Update(Msg1):Id=" + session.getIdentifier(msg) + ", msgText=" + msg.toString());
		System.out.println("Update(Msg2):Id=" + session.getIdentifier(msg2) + ", msgText=" + msg2.toString());
		
	}
}
